﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Windows.Forms;
using PayexpDAL;
using Utilities;
using System.Globalization;
using ExcelHandler;

namespace Moneymatters
{
    public partial class MainForm
    {
        #region Private Fields
        #endregion Private Fields

        #region Properties
        #endregion Properties

        #region Private Methods

        private void DisplayHistory(int transctionMasterId)
        {
            List<TransactionMasterHistory> requiredHistoryList =
                this.dataAccessHandlerCommonInstance.GetTransactionMasterHistoryByTransactionMasterId(transctionMasterId, MainForm.Context);

            if (requiredHistoryList.Count == 0)
            {
                MessageBox.Show("No history found for the selected transaction.", "No History Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            this.tabControl.SelectedTab = this.tabControl.TabPages["tabPageViewHistory"];
            this.dataGridViewHistory.Rows.Clear();

            this.labelHistoryViewTransactionMasterId.Text = "Transaction Master Id: " + requiredHistoryList.First().TransactionMasterId.ToString();
            this.labelHistoryViewTransactionHead.Text = "Transaction Head: " + requiredHistoryList.First().HeadDisplayName;
            this.labelHistoryViewTransactionMode.Text = "Transaction Mode: " + requiredHistoryList.First().TransactionModeValueDisplayName;

            if (requiredHistoryList.First().Head.Equals("head_expense"))
            {
                this.labelHistoryViewVoucherReceiptNumber.Text = "Voucher #: " + requiredHistoryList.First().VoucherReceiptPrefix + requiredHistoryList.First().VoucherReceiptNumber.ToString();
            }
            else
            {
                this.labelHistoryViewVoucherReceiptNumber.Text = "Receipt #: " + requiredHistoryList.First().VoucherReceiptPrefix + requiredHistoryList.First().VoucherReceiptNumber.ToString();
            }

            foreach (TransactionMasterHistory transactionMasterHistory in requiredHistoryList)
            {
                this.dataGridViewHistory.Rows.Add();
                int newRowIndex = this.dataGridViewHistory.Rows.Count - 1;
                DataGridViewRow newRow = this.dataGridViewHistory.Rows[newRowIndex];

                if (transactionMasterHistory.Head.Equals("head_expense"))
                {
                    newRow.DefaultCellStyle.BackColor = Color.PaleVioletRed;
                }
                else
                {
                    newRow.DefaultCellStyle.BackColor = Color.LightGreen;
                }

                newRow.Cells["dgViewHistoryVersionNumber"].Value = transactionMasterHistory.VersionNumer;

                newRow.Cells["dgViewHistoryTransactionDate"].Value = transactionMasterHistory.TransactionDate.Get_DD_MONTH_YY_DateString();//.ToShortDateString();

                if (transactionMasterHistory.BankDate.HasValue)
                {
                    newRow.Cells["dgViewHistoryBankDate"].Value = transactionMasterHistory.BankDate.Value.Get_DD_MONTH_YY_DateString();
                }

                newRow.Cells["dgViewHistoryChequeDetails"].Value = transactionMasterHistory.GetConsolidatedChequeDetails();

                newRow.Cells["dgViewHistoryTransactionBank"].Value = GetBankName(transactionMasterHistory.BankAccountsId);

                newRow.Cells["dgViewHistoryTransctionAmount"].Value = transactionMasterHistory.TransactionAmount;

                newRow.Cells["dgViewHistoryNarration"].Value = transactionMasterHistory.Narration;

                newRow.Cells["dgViewHistoryHeadDetails"].Value = transactionMasterHistory.GetHeadDetailsComplete();

                newRow.Cells["dgViewHistoryUpdatedByDetails"].Value = transactionMasterHistory.UpdatedBy + " " + transactionMasterHistory.UpdatedDate.Get_DD_MONTH_YY_DateString();

                newRow.Cells["dgViewHistoryActionTaken"].Value = transactionMasterHistory.ActionTaken;

                newRow.Cells["dgViewHistoryExpenseApproverNames"].Value = transactionMasterHistory.ExpenseApprovalDetails;

                newRow.Cells["dgViewHistoryChangedFields"].Value = string.Join(", ", transactionMasterHistory.ChangeDetails.ToArray());

                foreach (DataGridViewCell cell in newRow.Cells)
                {
                    cell.Style.WrapMode = DataGridViewTriState.True;
                }
            }
        }

        

        void dataGridViewHistory_Click(object sender, EventArgs e)
        {
            DataGridView dataGridView = (DataGridView)sender;

            if (dataGridView.SelectedRows.Count == 0)
            {
                return;
            }

            DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
            selectedRow.DefaultCellStyle.SelectionBackColor = selectedRow.DefaultCellStyle.BackColor;
        }

        #endregion Private Methods

        #region Public Methods
        #endregion Public Methods
    }
}